﻿using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace Domain.Models
{
    public class BinarySaver<TEntity> : IRepository<TEntity>
    {
        private readonly BinaryFormatter binaryFormatter;

        public BinarySaver()
        {
            binaryFormatter = new BinaryFormatter();
        }

        public void Save(TEntity variant, string fileName)
        {
            using (var fileStream = new FileStream(fileName, FileMode.Create))
                binaryFormatter.Serialize(fileStream, variant);
        }

        public TEntity Load(string fileName)
        {
            using (var fileStream = new FileStream(fileName, FileMode.OpenOrCreate))
                return (TEntity)binaryFormatter.Deserialize(fileStream);
        }
    }
}